<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证号" prop="certno">
        <el-input
          v-model="queryParams.certno"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报名赛事" prop="competitionId">
          <el-select v-model="queryParams.competitionId" placeholder="请选择赛事">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
          <!--          <el-input v-model="form.competitionId" placeholder="请输入报名赛事" />-->
        <!--        <el-input-->
        <!--          v-model="queryParams.competitionId"-->
        <!--          placeholder="请输入报名赛事"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
      </el-form-item>
<!--      <el-form-item label="考场编号" prop="examAddress">-->
<!--        <el-input-->
<!--          v-model="queryParams.examAddress"-->
<!--          placeholder="请输入考场编号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="座位号" prop="examSeatnumber">-->
<!--        <el-input-->
<!--          v-model="queryParams.examSeatnumber"-->
<!--          placeholder="请输入考场座位号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="审核时间" prop="examReviewtime">
        <el-date-picker clearable
          v-model="queryParams.examReviewtime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择成绩审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:exam:exam:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:exam:exam:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:exam:exam:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:exam:exam:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:exam:exam:import']"
        >导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="examList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center" prop="name" width="175" />
      <el-table-column label="身份证号码" align="center" prop="certno"  width="200"/>
      <el-table-column label="报名赛事" align="center" prop="competitionId" width="300"/>
      <el-table-column label="考场编号" align="center" prop="examAddress" />
      <el-table-column label="考场座位号" align="center" prop="examSeatnumber" />
      <el-table-column label="考试成绩" align="center" prop="examScores" width="150" />
      <el-table-column label="成绩审核" align="center" prop="examReview">
        <template slot-scope="scope">
          <span v-if="scope.row.examReview == 1" style="color: forestgreen;">通过</span>
          <span v-if="scope.row.examReview == 0" style="color: #dc9747;">未审核</span>
          <span v-if="scope.row.examReview == 2" style="color: red;">不通过</span>
          <span v-if="scope.row.examReview == 3" style="color:  red;">异常</span>
        </template>
      </el-table-column>

      <el-table-column label="成绩审核时间" align="center" prop="examReviewtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examReviewtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="成绩审核人id" align="center" prop="examReviewId" />-->
      <el-table-column label="成绩发布状态" align="center" prop="examStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.examStatus == 1" style="color: forestgreen;">发布</span>
          <span v-if="scope.row.examStatus == 0" style="color: #eacf22;">未发布</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit" round
            @click="handleReview(scope.row)"
            v-hasPermi="['system:exam:exam:review']"
          >审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
          <el-row style="color: #0b7596">点击导出按钮可获取模板!</el-row>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改成绩管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考场编号" prop="examAddress">
          <el-input v-model="form.examAddress" placeholder="请输入考场编号" readonly/>
        </el-form-item>
        <el-form-item label="座位号" prop="examSeatnumber">
          <el-input v-model="form.examSeatnumber" placeholder="请输入考场座位号" readonly />
        </el-form-item>
        <el-form-item label="考试成绩" prop="examScores">
          <el-input v-model="form.examScores" placeholder="请输入考试成绩" />
        </el-form-item>
<!--        <el-form-item label="成绩审核时间" prop="examReviewtime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.examReviewtime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择成绩审核时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="成绩审核人id" prop="examReviewId">-->
<!--          <el-input v-model="form.examReviewId" placeholder="请输入成绩审核人id" readonly />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 成绩审核对话框 -->
    <el-dialog :title="title" :visible.sync="review" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考场编号" prop="examAddress">
          <el-input v-model="form.examAddress" placeholder="请输入考场编号" readonly/>
        </el-form-item>
        <el-form-item label="座位号" prop="examSeatnumber">
          <el-input v-model="form.examSeatnumber" placeholder="请输入考场座位号" readonly />
        </el-form-item>
        <el-form-item label="考试成绩" prop="examScores">
          <el-input v-model="form.examScores" placeholder="请输入考试成绩" readonly/>
        </el-form-item>
        <el-form-item label="审核结果" prop="examReview">
          <el-select v-model="form.examReview" placeholder="请选择审核结果">
            <el-option
              v-for="item in reviewoptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              :disabled="item.disabled">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addExam, delExam, getExam, listExam, updateExam} from "@/api/exam/exam";
import { getToken } from "@/utils/auth";
import {loadCompetition} from "@/api/student/student";

export default {
  name: "Exam",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 成绩管理表格数据
      examList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      review: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        certno: null,
        competitionId: null,
        examAddress: null,
        examSeatnumber: null,
        examScores: null,
        examReview: null,
        examReviewtime: null,
        examReviewBy: null,
        examStatus: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      options: [],
      // 表单校验
      rules: {
        examScores:[
          { required: true, message: "成绩不能为空", trigger: "change" }
        ],
        competitionId: [
          { required: true, message: "报名赛事id不能为空", trigger: "change" }
        ],
        examReview: [
          { required: true, message: "成绩审核(默认0未审核,1通过,2不通过,3.异常)不能为空", trigger: "change" }
        ],
        examSeatnumber: [
          { required: true, message: "座位号不能为空", trigger: "change" },
          { pattern: /^[\s\S]{1,10}座位$/, message: '请输入正确的格式: XX座位'}
        ],
        examAddress: [
          { required: true, message: "考场号不能为空", trigger: "change" },
          { pattern: /^[\s\S]{1,10}号考场$/, message: '请输入正确的格式: XX号考场'}
        ],
      },
      reviewoptions: [
        {id: '0', name: '未审核' , disabled: true},
        {id: '1', name: '审核通过'},
        {id: '2', name: '未通过'},
        {id: '3', name: '异常'},
      ],
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/exam/importData"
      },
    };
  },
  created() {
    this.getList();
    this.loadCompetition();
  },
  methods: {
    loadCompetition() {
      console.log("赛事下拉选加载");
      loadCompetition().then(response => {
        this.options = response.data
        // console.log(this.options);
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "用户导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/system/exam/importTemplate', {
      }, `exam_${new Date().getTime()}.xlsx`)
    },
// 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
// 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
// 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
      this.upload.open = false;
      this.$message({
        showClose: true,
        type: 'success',
        message: '开始处理!请稍后!'
      });
    },
    /** 查询成绩管理列表 */
    getList() {
      this.loading = true;
      listExam(this.queryParams).then(response => {
        this.examList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.review = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        certno: null,
        competitionId: null,
        examAddress: null,
        examSeatnumber: null,
        examScores: null,
        examReview: null,
        examReviewtime: null,
        examReviewBy: null,
        examStatus: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.names = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加成绩管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExam(id).then(response => {
        this.form = response.data;
        this.form.competitionId = Number(this.form.competitionId);
        this.open = true;
        this.title = "修改成绩管理";
      });
    },
    /**审核按钮操作*/
    handleReview(row) {
      this.reset();
      const id = row.id || this.ids
      const review = row.examReview || this.examReview
      const examScores = row.examScores || this.examScores
      if(review == 1) {
        this.$message({
          showClose: true,
          message: '已经审核通过啦!',
          type: 'warning'
        });
        return;
      }
      if(examScores == null) {
        this.$message({
          showClose: true,
          message: '成绩为空!不能审核!',
          type: 'error'
        });
        return;
      }
      getExam(id).then(response => {
        this.form = response.data;
        this.form.competitionId = Number(this.form.competitionId);
        this.form.examReview = String(this.form.examReview);
        this.review = true;
        this.title = "成绩审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExam(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.review = false;
              this.getList();
            });
          } else {
            addExam(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const names = row.name || this.names;
      this.$modal.confirm('是否确认删除名称为"' + names+ '"的数据项？').then(function() {
        return delExam(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      if(!(this.queryParams.competitionId == null)){
        this.download('system/exam/exportExam', {
          ...this.queryParams
        }, `成绩管理_${new Date().toLocaleString()}.xlsx`)
      }else {
        this.$modal.msgError("请先进行赛事筛选!");
        return;
      }

    }
  }
};
</script>
